<template>
	<view class="content">
		<scroll-view :scroll-y="true" class="item-content">
			<view class="item" v-for="(item,index) in list" :key="item.id">
				<view class="time-text-box" v-if="index === 0">
					<text class="time-text">{{item.time}}</text>
				</view>
				<view class="item-left" v-if="item.contactMethod !== obj.contactMethod">
					<view class="item-right-img" v-if="item.contactMethod === '0'">
						<u-avatar :src="obj.pb_info"></u-avatar>
					</view>
					<view class="item-right-img" v-else>
						<u-avatar text="U" fontSize="18"  :randomBgColor="false" ></u-avatar>
					</view>
					<view class="item-right-content">
						<view class="item-right-content-text">
							<u-parse :content="item.content" @imgTap="bindClick"></u-parse>
						</view>
					</view>
				</view>
				<view class="item-right" v-if="item.contactMethod === obj.contactMethod">
					<view class="item-right-box"></view>
					<view class="item-right-box2">
						<view class="item-right-content">
							<view class="item-right-content-text">
								<u-parse  :content="item.content" @imgTap="bindClick"></u-parse>
							</view>
						</view>
						<view class="item-right-img" v-if="item.contactMethod === '0'">
							<u-avatar :src="obj.pb_info"></u-avatar>
						</view>
						<view class="item-right-img" v-else>
							<u-avatar text="U" fontSize="18" :randomBgColor="false"></u-avatar>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="bottom-box">
			<view class="bottom-box-left">
				<u--input @input="userInput" placeholder="请输入内容" border="surround" v-model="inputVal" shape="circle">
				</u--input>
			</view>
			<view class="bottom-box-right">
				<view style="height: 30px;marginLeft: 10px;display: flex;justify-content: center;
			align-items: center;">
					<u-transition fade-right :show="!flagShow">
						<u-button :style="{height: '30px'}" type="success" @click="btnSend" text="发送"></u-button>
					</u-transition>
				</view>
				<u-transition fade-right :show="flagShow">
					<view class="plus" @click="addImage">
						<u-icon color="#000" size="18" name="plus"></u-icon>
					</view>
				</u-transition>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				inputVal: '',
				code: '',
				obj: {

				},
				list: [],
				number: 0,
				flagShow: true,
				isStartGetMessageFlag: null
			};
		},
		onLoad(options) {
			this.obj = JSON.parse(options.obj)
			this.getCode()
			this.getMessage()
		},
		onShow() {
			this.isStartGetMessageFlag = setInterval(() => {
				this.getMessage()
			},6000)
		},
		onUnload() {
			clearInterval(this.isStartGetMessageFlag)
		},
		methods: {
			bindClick(event) {
				console.log(event) 
				console.log(event) 
			},
			addImage() {
				uni.chooseImage({
					count: 1,
					sizeType: 'compressed',
					success: async (res) => {
						console.log(res)
						uni.showLoading()
						const filePath = res.tempFilePaths[0]
						const cloudPath = res.tempFilePaths[0].substring(11)
						const result = await uniCloud.uploadFile({
							filePath: filePath,
							cloudPath: cloudPath,
							onUploadProgress: function(progressEvent) {
								console.log(progressEvent);
								var percentCompleted = Math.round(
									(progressEvent.loaded * 100) / progressEvent.total
								);
							}
						});
						uni.hideLoading()
						let str =
							`<p class='testssss' data-value="${result.fileID}"><img   src="${result.fileID}" style="height:80px; width:80px"  /></p>`
						this.inputVal = str
						this.userAddMessage()

					},
					fail: (res) => {
						console.log(res)
					}
				})
			},
			userInput() {
				if (this.inputVal.trim().length !== 0) return this.flagShow = false
				else return this.flagShow = true
			},
			getMessage() {
				let _this = this
				uni.request({
					url: 'https://www.apricity.asia:8911/message/getMessageByPage',
					method: 'POST',
					data: this.obj,
					header: {
						Authorization: uni.getStorageSync('token')
					},
					success: (res) => {
						this.list = res.data.data
					},
					complete: () => {

					}
				})
			},
			getCode() {
				wx.login({
					success: (res) => {
						this.code = res.code
					}
				})
			},
			userPublicMessage() {
				console.log('ok')
				if (this.number !== 0) return
				let _this = this
				uni.requestSubscribeMessage({
					tmplIds: ['9hmKCuAZWLdf-NyGcC50X1Fg-KUFU4YJwwMLoI5KCwI'],
					success: async (res) => {
						if (res['9hmKCuAZWLdf-NyGcC50X1Fg-KUFU4YJwwMLoI5KCwI'] !== 'accept') return
						let prames = {
							code: _this.code,
							template_id: '9hmKCuAZWLdf-NyGcC50X1Fg-KUFU4YJwwMLoI5KCwI'
						}
						const {
							data: res2
						} = await uni.$http.post('/message/addWenXinMessage', prames)
						if (res2.status !== 200) return uni.showToast({
							icon: 'none',
							title: '订阅失败',
							duration: 2000
						})
					},
					fail: (res) => {
						console.log(res)
					},
					complete: (res) => {
						_this.number++
					}
				})
			},
			userAddMessage() {
				let _this = this
				uni.request({
					url: 'https://www.apricity.asia:8911/message/chatMessage',
					method: 'POST',
					data: {
						...this.obj,
						content: this.inputVal
					},
					header: {
						Authorization: uni.getStorageSync('token')
					},
					success: () => {

					},
					complete: () => {
						_this.inputVal = ''
						_this.getMessage()
					}
				})
			},
			btnSend() {
				if (this.inputVal.trim().length === 0) return uni.showToast({
					icon: 'none',
					title: '请输入',
					duration: 2000,
				})
				this.userPublicMessage()
				this.userAddMessage()
			}
		}
	};
</script>

<style lang="scss" scoped>
	/deep/ .custom-style {
		font-size: 18px;
		color: #FFF;
	}

	/deep/.u-input {
		background: #FFF !important;
	}

	.bottom-box {
		position: fixed;
		width: 100vw;
		z-index: 999;
		bottom: 0;
		left: 0;
		display: flex;
		padding: 5px 20px;
		justify-content: center;
		align-items: center;
		border-top: 1px solid #FCFCFC;
		background: #f3f3f3;

		.bottom-box-left {
			margin-right: 5px;

			.input {
				background: #FFF;
			}
		}

		.bottom-box-right {
			display: flex;
			justify-content: center;
			align-items: center;

			.plus {
				margin-left: 10px;
				border: 1px solid #000;
				border-radius: 50%;
				display: flex;
				height: 30px;
				width: 30px;
				justify-content: center;
				align-items: center;
			}
		}
	}

	.content {
		z-index: 0;
		height: 100vh;
		background: #f4f4f5;

		.time-text-box {
			color: #FFF;
			display: flex;
			margin: 20rpx 0;
			align-items: center;
			justify-content: center;

			.time-text {
				background: #C4C6C9;
				padding: 3px;
				font-size: 12px;
			}
		}

		.item-content {
			background: #f4f4f5;
			padding: 10px;
			margin-bottom: 50px;

			.item {
				.item-left {
					margin-top: 30rpx;
					display: flex;
					justify-content: left;

					.item-left-img {}

					.item-right-content {
						border-radius: 5px;
						margin-left: 15px;
						background: #FFF;
						padding: 5px 15px;
						font-size: 13px;
						max-width: 60vw;
						word-wrap: break-word;
						word-break: break-all;
						position: relative;
						z-index: 1;
						display: flex;
						align-items: center;
						justify-content: center;

						.item-right-content-text {
							z-index: 2;
						}
					}

					.item-right-content::before {
						content: '';
						left: -3%;
						top: 15%;
						width: 15px;
						height: 15px;
						z-index: 0;
						background: #FFF;
						transform: rotate(45deg);
						position: absolute;
					}
				}

				.item-right {
					margin-top: 30rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.item-right-box {}

					.item-right-box2 {
						display: flex;

						justify-content: center;

						.item-right-img {}

						.item-right-content {
							border-radius: 5px;
							margin-right: 15px;
							background: #53c21d;
							padding: 5px 15px;
							font-size: 13px;
							max-width: 60vw;
							word-wrap: break-word;
							word-break: break-all;
							position: relative;
							z-index: 1;
							display: flex;
							align-items: center;
							justify-content: center;

							.item-right-content-text {
								z-index: 2;

							}
						}

						.item-right-content::after {
							content: '';
							right: -3%;
							top: 15%;
							width: 15px;
							height: 15px;
							z-index: 0;
							background: #53c21d;
							transform: rotate(45deg);
							position: absolute;
						}
					}
				}
			}
		}
	}
</style>
